using System.Windows;
using System.Windows.Controls;

namespace PrismNavigationFramework.Navigation.Controls
{
    public class TransitionContentControl : ContentControl
    {
		private ContentPresenter CurrentContentPresentationSite;
		private ContentPresenter PreviousContentPresentationSite;

        public TransitionContentControl()
		{
            DefaultStyleKey = typeof(TransitionContentControl);
		}

		public override void OnApplyTemplate()
		{			
			base.OnApplyTemplate();

			PreviousContentPresentationSite = GetTemplateChild("PreviousContentPresentationSite") as ContentPresenter;
			CurrentContentPresentationSite = GetTemplateChild("CurrentContentPresentationSite") as ContentPresenter;
			
			if (CurrentContentPresentationSite != null)
			{
				CurrentContentPresentationSite.Content = Content;
			}
		}

		protected override void OnContentChanged(object oldContent, object newContent)
		{
			base.OnContentChanged(oldContent, newContent);

			if ((CurrentContentPresentationSite != null) && (PreviousContentPresentationSite != null))
			{
				CurrentContentPresentationSite.Content = newContent;
				PreviousContentPresentationSite.Content = oldContent;

				VisualStateManager.GoToState(this, "Normal", false);
				VisualStateManager.GoToState(this, "Transition", true);
			}
		}       
    }
}